package com.ok.prize.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ok.common.domain.po.Result;
import com.ok.prize.domain.dto.OkPrizeDTO;
import com.ok.prize.domain.po.OkPrize;
import com.ok.prize.service.IOkPrizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 奖品 前端控制器
 * </p>
 *
 * @author lhw
 * @since 2024-05-06
 */
@Api(tags = "奖品相关接口")
@RestController
@RequestMapping("/prize")
@RequiredArgsConstructor
public class OkPrizeController {

    private final IOkPrizeService okPrizeService;

    @ApiOperation(value = "添加奖品")
    @PostMapping("/add")
    public Result add(@RequestBody  OkPrizeDTO prizeDTO){
        OkPrize okPrize = new OkPrize();
        BeanUtils.copyProperties(prizeDTO,okPrize);
        okPrizeService.save(okPrize);
        return Result.success("添加成功");
    }

    @ApiOperation(value = "删除奖品")
    @DeleteMapping("/delete")
    public Result delete(Integer id){
        okPrizeService.removeById(id);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "修改奖品")
    @PutMapping("/update")
    public Result update(@RequestBody OkPrize prize){
        okPrizeService.updateById(prize);
        return Result.success("修改成功");
    }

    @ApiOperation(value = "分页查询奖品")
    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNo,@RequestParam Integer pageSize,
                       @RequestParam(required = false)String name){
        Page<OkPrize> pageInfo = okPrizeService.pageWith(pageNo,pageSize,name);

        return Result.success(pageInfo);
    }

    @ApiOperation(value = "根据id查询奖品")
    @GetMapping("/prizeInfo")
    public Result<OkPrize> prizeInfo(@RequestParam Integer id){
        return Result.success(okPrizeService.getById(id));
    }
}
